package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.service.cargo.ExtCproductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ExtCproductServiceImpl implements ExtCproductService {

    @Autowired
    private ExtCproductDao extCproductDao;

    @Autowired
    private ContractDao contractDao;

    /*
        附件分页查询
     */
    @Override
    public PageInfo<ExtCproduct> findByPage(ExtCproductExample extCproductExample, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        //查询数据
        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);

        //得到附件页面
        PageInfo<ExtCproduct> pageInfo = new PageInfo<>(extCproductList);


        return pageInfo;
    }

    /*
     附件条件查询
  */
    @Override
    public List<ExtCproduct> findAll(ExtCproductExample extCproductExample) {
        List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);
        return extCproductList;
    }

      /*
        根据id查询附件
    */

    @Override
    public ExtCproduct findById(String id) {

        return extCproductDao.selectByPrimaryKey(id);
    }

       /*
      添加附件
  */

    @Override
    public void save(ExtCproduct extCproduct) {

        extCproduct.setId(UUID.randomUUID().toString());
        //有两个字段不能为空
        extCproduct.setCreateTime(new Date());
        extCproduct.setUpdateTime(new Date());
//        1. 计算出附件的总价

        double amount =0 ;
        if (extCproduct.getPrice() != null && extCproduct.getCnumber() != null) {
            amount = extCproduct.getCnumber() * extCproduct.getPrice();
            extCproduct.setAmount(amount);
        }
//        2. 插入附件数据
        extCproductDao.insertSelective(extCproduct);

        //        3.修改购销合同的总价   购销合同总价 = 购销合同原价格+附件的总价
        //找到购销合同

        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());

        //        4. 修改购销合同的附件种类数量
        if (contract.getExtNum() != null) {
            contract.setExtNum(contract.getExtNum() + 1);
        } else {
            //第一次添加附件
            contract.setExtNum(1);
        }
        //        5. 更新购销合同

        contractDao.updateByPrimaryKeySelective(contract);

    }

    /*
 更新附件
  */
    @Override
    public void update(ExtCproduct extCproduct) {

        ExtCproduct oldExtCproduct = extCproductDao.selectByPrimaryKey(extCproduct.getId());

        //修改更新的时间
        extCproduct.setUpdateTime(new Date());
        //        1. 计算附件的总价
        double amount = 0;
        if (extCproduct.getPrice() != null && extCproduct.getCnumber() != null) {
            amount = extCproduct.getCnumber() * extCproduct.getPrice();
            extCproduct.setAmount(amount);
        }

        //        2. 更新附件
        extCproductDao.updateByPrimaryKeySelective(extCproduct);

        //        3. 重新计算购销合同总价   购销合同总价 = 购销合同原总价-原附件总价+新总价
        Contract contract = contractDao.selectByPrimaryKey(extCproduct.getContractId());

        contract.setTotalAmount(contract.getTotalAmount() - oldExtCproduct.getAmount() + amount);

//        4. 更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);


    }

     /*
    根据主键删除附件
     */

    @Override
    public void delete(String id) {
        //        1. 根据id找附件
        ExtCproduct dbExtCproduct = extCproductDao.selectByPrimaryKey(id);//从数据库中读取出来存放内存
//        2. 删除附件

        extCproductDao.deleteByPrimaryKey(id);

        //        3. 更新购销合同总价
        //找到购销合同
        Contract contract = contractDao.selectByPrimaryKey(dbExtCproduct.getContractId());

        contract.setTotalAmount(contract.getTotalAmount()-dbExtCproduct.getAmount());

        //        4. 更新购销合同种类
        contract.setExtNum(contract.getExtNum()-1);

        //        5. 更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);

    }


}
